from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
# 配置数据库URI
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@192.168.0.189:3306/shale-gas'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)


# 定义数据库模型

class GasProductionPlatformPre(db.Model):
    __tablename__ = 'gas_production_platform_pre'

    id = db.Column(db.String(64), primary_key=True)
    platform_id = db.Column(db.String(64), nullable=True, comment='平台基础表id')
    platform_no = db.Column(db.String(32), nullable=True, comment='平台号')
    collect_date = db.Column(db.Date, nullable=True, comment='日期')
    pre_value = db.Column(db.Float, nullable=True, comment='气量预测值')
    pressure_transport = db.Column(db.Float, nullable=True, comment='输气压力')


class GasProductionWellPre(db.Model):
    __tablename__ = 'gas_production_well_pre'

    id = db.Column(db.String(64), primary_key=True)
    well_no = db.Column(db.String(32), nullable=True, comment='井号')
    platform_no = db.Column(db.String(32), nullable=True, comment='井号对应的平台号')
    collect_date = db.Column(db.Date, nullable=True, comment='日期')
    cover_pre_value = db.Column(db.Float, nullable=True, comment='套压预测值')
    oil_pre_value = db.Column(db.Float, nullable=True, comment='油压预测值')
    pro_days = db.Column(db.Integer, nullable=True, comment='已生产天数')


class GasPlatformCompressor(db.Model):
    __tablename__ = 'gas_platform_compressor'

    # 主键，假设原表中应该有一个名为 id 的字段，但原 SQL 中未给出
    # 如果表中确实没有 id 字段，你需要添加一个，或者指定另一个字段为主键
    id = db.Column(db.String(64), primary_key=True)  # 假设主键是字符串类型

    platform_id = db.Column(db.String(64), nullable=True, comment='平台id')
    platform_no = db.Column(db.String(64), nullable=True, comment='平台号')
    originally_compressor_id = db.Column(db.String(64), nullable=True, comment='原压缩机id')
    originally_compressor_name = db.Column(db.String(64), nullable=True, comment='原压缩机名称')
    now_compressor_id = db.Column(db.String(64), nullable=True, comment='现压缩机id')
    now_compressor_name = db.Column(db.String(64), nullable=True, comment='现压缩机名称')
    originally_compressor_power = db.Column(db.Float, nullable=True, comment='原压缩机功率')
    now_compressor_power = db.Column(db.Float, nullable=True, comment='现压缩机功率')
    originally_procurement_costs = db.Column(db.Float, nullable=True, comment='原租赁成本')
    now_procurement_costs = db.Column(db.Float, nullable=True, comment='现租赁成本')
    day_save_money = db.Column(db.Float, nullable=True, comment='日节约用费')

class GasProductionWell(db.Model):
    id = db.Column(db.String, primary_key=True)
    well_no = db.Column(db.String)
    collect_date = db.Column(db.Date)
    cover_pressure = db.Column(db.Float)
    oil_pressure = db.Column(db.Float)
    production_gas_day = db.Column(db.Float)


class GasPlatformProduction(db.Model):
    id = db.Column(db.String, primary_key=True)  # 这里 id 是主键
    platform_id = db.Column(db.String)
    platform_no = db.Column(db.String)
    collect_date = db.Column(db.Date)  # 日期
    production_gas_year = db.Column(db.Float)  # 产气量
    pressure_transport = db.Column(db.Float)  # 输气压力
    # 可以添加其他方法和属性


class GasProductionIncrease(db.Model):
    id = db.Column(db.String, primary_key=True)  # 这里 id 是主键
    well_no = db.Column(db.String)
    platform_no = db.Column(db.String)
    begin_time = db.Column(db.Date)
    end_time = db.Column(db.Date)
    days = db.Column(db.String)
    before_pro = db.Column(db.Float)
    after_pro = db.Column(db.Float)
    amplify = db.Column(db.Float)
    absolute_inc = db.Column(db.Float)
    production_inc = db.Column(db.Float)
    type = db.Column(db.String)
    # 可以添加其他方法和属性


class GasMeasuresCostManagement(db.Model):
    platform_no = db.Column(db.String, primary_key=True)
    bubble_row_cost = db.Column(db.Float)
    continuous_gas_lift_cost = db.Column(db.Float)
    intermittent_gas_lift_cost = db.Column(db.Float)
    pressurization_costs = db.Column(db.Float)


class GasBaseCompressor(db.Model):  # 压缩机
    id = db.Column(db.String, primary_key=True)
    update_by = db.Column(db.String, default='', nullable=False, comment='更新人')
    update_time = db.Column(db.DateTime, comment='更新日期')
    sys_org_code = db.Column(db.String, default='', nullable=False, comment='所属部门')
    create_by = db.Column(db.String, default='', nullable=False, comment='创建人')
    create_time = db.Column(db.DateTime, comment='创建日期')
    group_model = db.Column(db.String, comment='机组型号')
    compressor_model = db.Column(db.String, comment='压缩机型号')
    intake_pressure_min = db.Column(db.Float, comment='最小进气压力Mpa')
    intake_pressure_max = db.Column(db.Float, comment='最大进气压力Mpa')
    intake_pressure_optimal = db.Column(db.String, comment='进气压力最优')
    intake_temperature = db.Column(db.Float, comment='进气温度℃')
    exhaust_gas_min = db.Column(db.Float, comment='最小排气量Nm3/d')
    exhaust_gas_max = db.Column(db.Float, comment='最大排气量Nm3/d')
    exhaust_pressure = db.Column(db.Float, comment='排气压力Mpa')
    exhaust_temperature = db.Column(db.Float, comment='排气温度℃')
    compressor_columns = db.Column(db.Float, comment='压缩机列数')
    compressor_cylinders = db.Column(db.Float, comment='压缩机气缸数')
    rated_power = db.Column(db.Float, comment='电机额定功率kw')
    rated_rotate_speed = db.Column(db.Float, comment='电机额定转速rpm')
    weight = db.Column(db.Float, comment='重量')
    size = db.Column(db.String, comment='尺寸(长×宽×高)')
    lease_costs = db.Column(db.Float, comment='租赁成本')


class GasCompressorWorkcondition(db.Model):  # 压缩机工况表
    id = db.Column(db.String, primary_key=True)
    update_by = db.Column(db.String, default='', nullable=False, comment='更新人')
    update_time = db.Column(db.DateTime, comment='更新日期')
    sys_org_code = db.Column(db.String, default='', nullable=False, comment='所属部门')
    create_by = db.Column(db.String, default='', nullable=False, comment='创建人')
    create_time = db.Column(db.DateTime, comment='创建日期')
    compressor_id = db.Column(db.String, comment='所属压缩机组(关联主键)')
    rotate_speed = db.Column(db.Float, comment='转速rpm')
    grade1_intake_pressure = db.Column(db.Float, comment='一级进气压力mpa')
    intake_temperature = db.Column(db.Float, comment='进气温度℃')
    exhaust_gas = db.Column(db.Float, comment='排气量Nm3/d')
    power = db.Column(db.Float, comment='功率kw')
    power_load_rate = db.Column(db.Float, comment='轴功率负荷率%')
    grade1_exhaust_pressure = db.Column(db.Float, comment='一级排气压力')
    grade2_exhaust_pressure = db.Column(db.Float, comment='二级排气压力')
    grade3_exhaust_pressure = db.Column(db.Float, comment='三级排气压力')
    exhaust_temperature = db.Column(db.Float, comment='排气温度')


# 定义数据库模型
class GasBasePlatform(db.Model):
    id = db.Column(db.String, primary_key=True)
    platform_no = db.Column(db.String, unique=True, nullable=False)
    lng = db.Column(db.Float, nullable=False)
    lat = db.Column(db.Float, nullable=False)


class GasBaseSite(db.Model):
    id = db.Column(db.String, primary_key=True)
    site_name = db.Column(db.String)
    lng = db.Column(db.Float)
    lat = db.Column(db.Float)


class GasBaseWell(db.Model):
    id = db.Column(db.String, primary_key=True)
    well_no = db.Column(db.String, nullable=False)
    lng = db.Column(db.Float, nullable=False)
    lat = db.Column(db.Float, nullable=False)
    is_production = db.Column(db.String)


class GasPlarformWellConnect(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    platform_id = db.Column(db.Integer, db.ForeignKey('gas_base_platform.id'), nullable=False)
    well_id = db.Column(db.Integer, db.ForeignKey('gas_base_well.id'), nullable=False)
    platform = db.relationship("GasBasePlatform", back_populates="wells")
    well = db.relationship("GasBaseWell", back_populates="platforms")


# 在GasBasePlatform模型中添加反向关系
GasBasePlatform.wells = db.relationship("GasPlarformWellConnect", back_populates="platform")
# 在GasBaseWell模型中添加反向关系
GasBaseWell.platforms = db.relationship("GasPlarformWellConnect", back_populates="well")
